
//全局数据
var control;
var root = window.player;
var songList;
var len;
var audio = root.audioManager;

var timer;

function getData(url){
	$.ajax({
		type:"GET",
		url:url,
		success:function(data){
			// console.log(data);
			len = data.length;
			//切换歌曲
			control = new root.controlIndex(len);
			songList = data;
			//加载歌曲（信息 图片）
			root.render(data[0]);
			//加载歌曲
			audio.getAudio(data[0])
			root.pro.renderAllTime(songList[0].duration)
			bindEvent()
			bindTouch()
		},
		error:function(){
			console.log("error")
		},
	})
	function bindEvent(){
		//合并 (prev next) 相同的操作
		$('body').on('play:change',function(e,index){
			//加载音频
			audio.getAudio(songList[index])
			//渲染时间
			root.pro.renderAllTime(songList[index].duration)
			root.render(songList[index])
			if(audio.status === 'play'){
				//切歌后旋转角度归零
				rotated(0)
				audio.play()
				root.pro.start(0)
				clearInterval(timer)
			}else{
				rotated(0)
				audio.pause()
				root.pro.stop()
				clearInterval(timer)
				root.pro.update(0)
			}
			$('.img-box').attr('data-deg',0);
			$('.img-box').css({
				'transform': 'rotateZ(' + 0 + 'deg)',
				'transition': 'none'
			})
		});
		$('.prev').on('click', function() {
			//放到 indexControl 模块
			/*if(this.index === 0) {
				this.index = len - 1;
			} else{
				this.index --;
			}
			return this.index*/

			//先改变index值 再去触发’play change‘

			var i = control.prev()
			$('body').trigger('play:change',i)

		});
		$('.next').on('click', function() {
			/*if(this.index === len - 1) {
				this.index = 0
			} else {
				this.index ++;
			}
			return this.index*/

			var i = control.next()

			/*audio.getAudio(songList[i])
			root.render(songList[i])
			if(audio.status === 'play'){
				audio.play()
			}*/
			$('body').trigger('play:change',i)
		})
		$('.play').on('click', function() {
			if(audio.status === 'pause'){
				// console.log(audio.status)
				audio.play();
				root.pro.start()
				var deg = $('.img-box').attr('data-deg');
				rotated(deg)
			}else{
				audio.pause()
				//暂停后图片停止旋转
				clearInterval(timer)
				//暂停后左侧时间停止计时
				root.pro.stop()
			}
			$('.play').toggleClass('playing')
		})

		$('.list').on('click',function() {

			root.playList.show(control)
		})
	}
	//进度条拖拽
	function bindTouch(){
		var left = $('.pro-bottom').offset().left;
		var width = $('.pro-bottom').offset().width;
		$('.slider-point').on('touchstart', function() {
			//点击拖拽就立马停止运动
			root.pro.stop()
		}).on('touchmove',function(e) {
			// console.log(e);
			//相对于浏览器宽度
			var x = e.changedTouches[0].clientX;
			//宽度百分比
			var per = (x - left) / width;
			//限制拖动范围
			if(per >= 0 && per <= 1){
			root.pro.update(per)
			}
		}).on('touchend',function(e) {
			var x = e.changedTouches[0].clientX;
			var per = (x - left) / width;
			if(per >= 0 && per <= 1){
				//获得当前时间
				var duration = songList[control.index].duration;
				var curTime = per * duration;
				//跳转歌曲
				audio.playTo(curTime);
				//标记当前音乐为播放
				$('.play').addClass('playing');
				audio.status = 'playing';
				root.pro.start(per)
			}
		})
	}
	//图片旋转
	function rotated(deg){
		//两个地方都调用了rotated  先清除，防止叠加
		clearInterval(timer);
		deg = +deg;
		timer = setInterval(function() {
			deg += 2;
			$('.img-box').attr('data-deg',deg);
			$('.img-box').css({
				'transform': 'rotateZ(' + deg + 'deg)',
				'transition': 'all 1s ease-out'
			})
		},200)
	}

}
getData('../mock/data.json');